<template>
  <el-dialog v-model="visible" title="代付配置" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" label-width="120" label-position="left">
      <el-form-item label="自动提交到三方" prop="autoPush2Third">
        <el-switch v-model="dataForm.autoPush2Third" inline-prompt active-text="是" inactive-text="否" />
      </el-form-item>
      <el-form-item label="付款渠道" prop="channelIdList">
        <el-select v-model="dataForm.channelIdList" placeholder="付款渠道" clearable multiple>
          <el-option v-for="(item, index) in channelList" :key="index" :value="item.id" :label="item.name">{{ item.name }}</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  autoPush2Third: false,
  channelIdList: []
});

const rules = ref({
  autoPush2Third: [{ required: true, message: t("validate.required"), trigger: "change" }],
  channelIdList: [{ required: true, message: t("validate.required"), trigger: "change" }]
});
const channelList: any = ref([]);
const init = () => {
  visible.value = true;
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  getInfo();
};

// 获取信息
const getInfo = () => {
  // 获取代付配置
  baseService.get("/payment/setting/df_setting").then((res) => {
    Object.assign(dataForm, res.data);
  });

  // 获取渠道列表
  baseService.get("/third/fundthird/page").then((res) => {
    channelList.value = res.data.list;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) {
      return false;
    }
    try {
      await baseService.post("/payment/setting/df_setting", dataForm);
      visible.value = false;
      ElMessage.success("提交成功");
      dataFormRef.value.resetFields();
    } catch (error) {
      console.log("error: ", error);
    }
  });
};

defineExpose({
  init
});
</script>
